package com.zd.kids.common;

import android.app.Application;
import android.content.Context;
import android.media.AudioFormat;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.Utils;
import com.hyy.libray.baselibrary.BaseApplication;
import com.zlw.main.recorderlib.RecordManager;
import com.zlw.main.recorderlib.recorder.RecordConfig;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import androidx.multidex.MultiDex;

/**
 * @ClassName: com.hyy.supermarket
 * @Description:
 * @Author: hyy
 * @Date: 2020/4/27
 * @Time: 11:24 PM
 */
public class MyApplication extends Application {

    public static Context context;

    public static boolean isHasFlutter;

    @Override
    public void onCreate() {
        super.onCreate();
//        FlutterApplication.init(this);
        Utils.init(this);
        init(this);
    }

    public void init(Application application) {
        handleSSLHandshake();
        BaseApplication.getInstance().init(application);
        initRecord(application);
        context = application.getBaseContext();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }


    private void initRecord(Application application) {

        RecordManager.getInstance().init(application, true);

        RecordManager.getInstance().changeFormat(RecordConfig.RecordFormat.WAV);

        RecordManager.getInstance().changeRecordDir(Constants.RECORD_ROOT_PATH);

        RecordManager.getInstance().changeRecordConfig(RecordManager.getInstance().getRecordConfig().setSampleRate(16000));

        RecordManager.getInstance().changeRecordConfig(RecordManager.getInstance().getRecordConfig().setEncodingConfig(AudioFormat.ENCODING_PCM_16BIT));

    }

    public void handleSSLHandshake() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {

                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {

                }
            }};
            SSLContext sc = SSLContext.getInstance("TLS");
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (Exception ignored) {

        }
    }

}

